package com.tangyuan.common.util;

import com.tangyuan.model.Headlines;
import com.tangyuan.model.HeadlinesImages;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class HeadlinesImageUtil {

    public static List<Headlines> packagingImage(List<Headlines> list,List<HeadlinesImages> headlinesImages){
        if(CollectionUtils.isEmpty(list)){
            return list;
        }
        if(CollectionUtils.isEmpty(headlinesImages)){
            headlinesImages = new ArrayList<>();
        }
        Map<Long, List<HeadlinesImages>> hashMap = headlinesImages.stream().collect(Collectors.groupingBy(HeadlinesImages::getItemId));
        for (Headlines item : list) {

            List<String> content = new ArrayList<>();
            switch (item.getType()){
                case 0:
                    String[] split = item.getImages().split(";");
                    if (split.length > 3) {
                        for (int i = 2; i >= 0; i--) {
                            content.add(split[i]);
                        }
                    } else {
                        for (String image : split) {
                            content.add(image);
                        }
                    }
                    break;
                case 1:
                    List<HeadlinesImages> images = hashMap.get(item.getId());
                    if(CollectionUtils.isEmpty(images)){
                        continue;
                    }
                    content = images.stream().map(HeadlinesImages::getImage).collect(Collectors.toList());
                    break;
                case 2:
                    content.add(item.getContent());
                    break;
            }
            if(CollectionUtils.isEmpty(content)){
                content = new ArrayList<>();
            }
            item.setContants(content);
        }
        return list;
    }
}
